
library(rgdal)
library(tidyverse)
library(sf)
library(dplyr)

library(ggplot2)

setwd("/Users/uqalanau/Dropbox/AirPollutionCognition/Stata/") 


load("OrigData/zip2geo")


data<-read.csv("Data/mainsample.csv")

myvars <- c("zip2","anon_id")

forplot<- data[myvars]

rm(data)

# unique anon ids 
anon_zip <- as.data.frame(tapply(forplot$zip2, forplot$anon_id, first)) # 
ids <- unique(forplot$anon_id)
anon_zip[,2] <- ids 
anon_zip <- mutate(anon_zip,ones = 1)
names(anon_zip)[1] = "zip2"
names(anon_zip)[2] = "anon_id"
# add up users by zip 
zip_users <- as.data.frame(tapply(anon_zip$ones,anon_zip$zip2,sum))
ziplist  <- unique(forplot$zip2)

zip_users[,2] <- ziplist
names(zip_users)[1] = "players"
names(zip_users)[2] = "zip2"
     
zip2_users <- merge(zip2,zip_users,by.x = "zip2", by.y="zip2",all = TRUE)

rm(forplot)

myplot<-ggplot(zip2_users) + 
  geom_sf(aes(fill=players))+ 
  xlim(-130, -65) +
  ylim(25, 50) + 
  scale_fill_continuous(low="lightgrey" , high="black",na.value = "white")


myplot

dev.copy(pdf,"/Users/uqalanau/Dropbox/AirPollutionCognition/Stata/Results/Zip2UserMap.pdf")
dev.off()
